﻿using System;
using System.ComponentModel;
using System.Windows.Navigation;
using GalaSoft.MvvmLight.Messaging;
using GalaSoft.MvvmLight.Threading;
using WinSource.ViewModel;

namespace WinSource.Views
{
    public partial class ArticlePage
    {
        #region Fields

        private readonly ArticleViewModel _articleViewModel;

        #endregion

        #region Constructor

        public ArticlePage()
        {
            InitializeComponent();

            _articleViewModel = DataContext as ArticleViewModel;

            Messenger.Default.Register<ArticlePropertyChangedMessage>(this, action => DispatcherHelper.CheckBeginInvokeOnUI(UpdateBrowser));
        }

        #endregion

        #region PhoneApplicationPage Overrides

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            UpdateBrowser();
        }

        #endregion

        #region Private Methods

        private void UpdateBrowser()
        {
            try
            {
                WebBrowser.NavigateToString(_articleViewModel.Article.Description);
            }
            catch
            {
            }
        }

        #endregion
    }
}